From 8a96aed206c6de57b43ad734ed9de2bc286059de Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sun, 6 Dec 2020 17:18:50 -0600 Subject: [PATCH] route_prefix should not apply to *_page settings --- src/pgwui_server/pgwui_server.py | 7 +++-- tests/test_pgwui_server.py | 40 +++++++++----------------- tests/test_pgwui_server_integration.py | 21 ++++++++++---- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/pgwui_server/pgwui_server.py b/src/pgwui_server/pgwui_server.py index 46615f3..143b54d 100644 --- a/src/pgwui_server/pgwui_server.py +++ b/src/pgwui_server/pgwui_server.py @@ -209,11 +209,12 @@ def autoconfigurable_components(settings, components): def apply_component_defaults(settings, components): '''Apply component default settings to existing settings ''' - components_to_config = autoconfigurable_components(settings, components) + config = Configurator(settings=settings) + config.include('pgwui_common') + components_to_config = autoconfigurable_components(settings, components) rp = settings['pgwui'].get('route_prefix') - with Configurator(settings=settings, route_prefix=rp) as config: - config.include('pgwui_common') + with config.route_prefix_context(rp): for component in components_to_config: log.debug('Autoconfiguring PGWUI component: {}'.format(component)) config.include(component) diff --git a/tests/test_pgwui_server.py b/tests/test_pgwui_server.py index 7cd136b..2c76f9c 100644 --- a/tests/test_pgwui_server.py +++ b/tests/test_pgwui_server.py @@ -40,24 +40,8 @@ import pgwui_server.exceptions as server_ex pytestmark = pytest.mark.unittest -# Use contextlib.AbstractContextManager for Python >= 3.6 -# (Or, better, use the magic mock maker that's not yet integrated.) -class MockConfigurator(): - def __init__(self, **kwargs): - pass - - def __enter__(self): - return self - - def __exit__(self, *args): - pass - - def make_wsgi_app(self): - return 'wsgi_app' - - def include(self, *args): - pass - +MockConfigurator = testing.make_magicmock_fixture( + pgwui_server, 'Configurator') mock_find_pgwui_components = testing.make_mock_fixture( pgwui_common.plugin, 'find_pgwui_components') @@ -524,7 +508,8 @@ mock_autoconfigurable_components = testing.make_mock_fixture( # apply_component_defaults() -def test_apply_component_defaults(monkeypatch, caplog, +def test_apply_component_defaults(caplog, + MockConfigurator, mock_autoconfigurable_components, mock_add_routes): '''A configurator is returned, a debug log entry is made for @@ -534,11 +519,9 @@ def test_apply_component_defaults(monkeypatch, caplog, mock_autoconfigurable_components.return_value = \ ['pgwui_mock_component_name'] - monkeypatch.setattr(pgwui_server, 'Configurator', - MockConfigurator) result = pgwui_server.apply_component_defaults({'pgwui': {}}, []) - assert isinstance(result, MockConfigurator) + assert isinstance(result, type(MockConfigurator())) logs = caplog.record_tuples @@ -567,11 +550,16 @@ def test_pgwui_server_config( assert result == test_configurator +mock_pgwui_server_config = testing.make_mock_fixture( + pgwui_server, 'pgwui_server_config') + + # main() -def test_main(monkeypatch): +def test_main(MockConfigurator, mock_pgwui_server_config): '''Returns a wsgi app''' - monkeypatch.setattr(pgwui_server, 'pgwui_server_config', - lambda *args: MockConfigurator()) - + config = MockConfigurator() + mock_pgwui_server_config.return_value = config + config.make_wsgi_app.return_value = 'wsgi_app' result = pgwui_server.main({}) + assert result == 'wsgi_app' diff --git a/tests/test_pgwui_server_integration.py b/tests/test_pgwui_server_integration.py index c1a5e4e..721c6a0 100644 --- a/tests/test_pgwui_server_integration.py +++ b/tests/test_pgwui_server_integration.py @@ -74,7 +74,10 @@ def test_main_integrated(): def check_route(config, name, expected): route_i = config.introspector.get('routes', name) - assert route_i['pattern'] == expected + if route_i is None: + assert expected is None + else: + assert route_i['pattern'] == expected def updated_dict(old, new): @@ -84,16 +87,21 @@ def updated_dict(old, new): @pytest.mark.parametrize( - ('settings', 'logout_path', 'menu_path'), [ + ('settings', 'logout_path', 'menu_path', 'menu_page_path'), [ (REFERENCE_SETTINGS, pgwui_logout.pgwui_logout.DEFAULT_LOGOUT_ROUTE, - pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE), + pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE, + None), (updated_dict(REFERENCE_SETTINGS, - {'pgwui.route_prefix': '/foo'}), + {'pgwui.route_prefix': '/foo', + 'pgwui.menu_page': {'type': 'file', + 'source': '/tmp/nofile.html', + 'url_path': '/menu'}}), 'foo' + pgwui_logout.pgwui_logout.DEFAULT_LOGOUT_ROUTE, - 'foo' + pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE)]) + 'foo' + pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE, + '/menu')]) def test_pgwui_server_config_no_route_prefix( - settings, logout_path, menu_path): + settings, logout_path, menu_path, menu_page_path): '''The given route_prefix is applied to the routes ''' config = pgwui_server.pgwui_server_config(settings) @@ -102,3 +110,4 @@ def test_pgwui_server_config_no_route_prefix( check_route(config, 'pgwui_logout', logout_path) check_route(config, 'pgwui_menu', menu_path) + check_route(config, 'pgwui_common.menu_page', menu_page_path) -- 2.34.1